//
//  oldEditInfoViewController.m
//  laonianbangSupport
//
//  Created by 勇拓 李 on 18/3/16.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldEditInfoViewController.h"
#import "oldPersonalInfoViewController.h"
#import "oldMyParentsViewController.h"

@interface oldEditInfoViewController ()
@property (weak, nonatomic) IBOutlet UITextView *text_view;

@end

@implementation oldEditInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    UIViewController *vc = self.navigationController.viewControllers[[self.navigationController.viewControllers count] - 2];
    
    if ([vc isKindOfClass:[oldPersonalInfoViewController class]]) {
        if([ROLE  isEqual: agedCareOld])
        {
            self.navigationItem.title = @"健康信息";
        }
        else
        {
            self.navigationItem.title = @"个人备注";
        }
    }
    else if ([vc isKindOfClass:[oldMyParentsViewController class]])
    {
        self.navigationItem.title = @"家属备注";
    }
    
    _text_view.text = _text;
    
    UIButton *rightbutton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
    
    //[leftbutton setBackgroundColor:[UIColor blackColor]];
    
    [rightbutton setTitle:@"保存" forState:UIControlStateNormal];
    
    [rightbutton addTarget:self action:@selector(rightbtnTapped:) forControlEvents:UIControlEventTouchDown];
    
    UIBarButtonItem *rightitem = [[UIBarButtonItem alloc]initWithCustomView:rightbutton];
    
    self.navigationItem.rightBarButtonItem = rightitem;
}

- (void)rightbtnTapped:(id)sender
{
    _text = _text_view.text;
    
    if (!_text) {
        _text = @"";
    }
    
    [oldHub waiting:@"保存中.."];
    
    WeakSelf
    
    NSString *key = @"introduce";
    
    if([ROLE  isEqual: agedCareOld])
    {
        key = @"ps";
    }
    
    NSCharacterSet  *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *text = [_text stringByTrimmingCharactersInSet:set];
    
    NSDictionary *params = [NSDictionary dictionaryWithObject:text forKey:key];
    
    [OLDNetWork httpRequestWithPostPut:URL_EditProfile params:params success:^(id data) {
        [oldHub hideHUD];
        [weakSelf editSuccess];
        ACCOUNT_SINGLETON.accountInfo = [[oldServer alloc] initWithDictionary:data];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

- (void)editSuccess
{
    UIViewController *vc = self.navigationController.viewControllers[[self.navigationController.viewControllers count] - 2];
    
    if ([vc isKindOfClass:[oldPersonalInfoViewController class]]) {
        oldPersonalInfoViewController *oldVc = (oldPersonalInfoViewController *)vc;
        oldVc.text = _text_view.text;
    }
    else if ([vc isKindOfClass:[oldMyParentsViewController class]])
    {
        oldMyParentsViewController *oldVc = (oldMyParentsViewController *)vc;
        oldVc.text = _text_view.text;
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
